{% extends "../base.html" %}

{% block title %} {% end %}
{% block css %}

{% end %}
{% block body %}

{% include ../common/header.html %}
<div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
        <div class="layui-tab layui-tab-brief" lay-filter="user">
            <ul class="layui-tab-title">
                <li><a href="login.html">登入</a></li>
                {% if reset_pwd=='1' %}
                <li class="layui-this">找回密码</li>
                {% else %}
                <li class="layui-this">重置密码</li>
                {% end %}
            </ul>
            <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
                <div class="layui-tab-item layui-show">
                    <!-- 重置密码 -->
                    {% if reset_pwd=='1' %}
                    <div class="layui-form layui-form-pane">
                        <form method="post">
                            {% raw xsrf_form_html() %}
                            <input type="hidden" name="rsa_encrypt" value="{{ rsa_encrypt }}">
                            <input type="hidden" name="action" value="email_reset_pwd">
                            <div class="layui-form-item">
                                <label for="L_email" class="layui-form-label">邮箱</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="L_account" name="account" required lay-verify="email" autocomplete="off" class="layui-input" value="">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="L_vercode" class="layui-form-label">验证码</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="code" class="layui-input">
                                </div>
                                <div class="layui-form-mid">
                                    <img id="code" src="/passport/captcha.png" style="width: 11.5em; cursor: pointer;" />
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn" alert="1" lay-filter="emailSubmit1" lay-submit>提交</button>
                            </div>
                        </form>
                    </div>
                    {% else %}
                    <div class="fly-msg">{{ username }}，请重置您的密码</div>
                    <div class="layui-form layui-form-pane"  style="margin-top: 30px;">
                        <form action="" method="post">
                            {% raw xsrf_form_html() %}
                            <input type="hidden" name="rsa_encrypt" value="{{ rsa_encrypt }}">
                            <input type="hidden" name="token" value="{{ token }}">
                            <div class="layui-form-item">
                                <label for="L_pass" class="layui-form-label">密码</label>
                                <div class="layui-input-inline">
                                    <input type="password" id="L_password" name="password" required lay-verify="password" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">8到40个任意字符</div>
                            </div>
                            <div class="layui-form-item">
                                <label for="L_repass" class="layui-form-label">确认密码</label>
                                <div class="layui-input-inline">
                                    <input type="password" id="L_repass" name="repass" required lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label for="L_vercode" class="layui-form-label">验证码</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="code" class="layui-input">
                                </div>
                                <div class="layui-form-mid">
                                    <img id="code" src="/passport/captcha.png" style="width: 11.5em; cursor: pointer;" />
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn" alert="1" lay-filter="formSubmit2" lay-submit>提交</button>
                            </div>
                        </form>
                    </div>
                    <div class="fly-error">该重置密码链接已失效，请重新校验您的信息</div>
                    <div class="fly-error">非法链接，请重新校验您的信息</div>
                    {% end %}
                </div>
            </div>
        </div>
    </div>
</div>
{% include ../common/footer.html %}

{% end %}

{% block javascript %}
<script src="{{ static_url('js/jsencrypt.js') }}" charset="utf-8"></script>
<script type="text/javascript">
var rsa_encrypt = '{{ rsa_encrypt }}'
var public_key = '{{ public_key.replace('\n', '')[26:-24] }}'
layui.use(['form'], function() {
    var $ = layui.jquery, form = layui.form

    // 表单验证
    form.verify({
        username : [/[A-Za-z0-9\u4e00-\u9fa5]{2,40}$/, '用户名必须2到40位字母、数字、汉字组合'],
        password : [/(.+){8,40}$/, '8到40个任意字符'],
        number : [/^[0-9]*$/, '必须输入数字啊']
    })
    {% if reset_pwd=='1' %}
    form.on('submit(emailSubmit1)', function(obj) {
        layer.msg('数据提交中...',{time:50000})

        var url = '/member/sendmail'

        $.ajax({
            type: "POST",
            url: url,
            data: obj.field,
            success: function(res) {
                if (res.code==0) {
                    layer.msg('已成功将激活链接发送到了您的邮箱，接受可能会稍有延迟，请注意查收。', {icon: 1, time: 2000})
                } else if(res.msg) {
                    layer.msg(res.msg)
                    $(":input[name='username']").focus()
                } else {
                    layer.msg('{{ _('未知错误') }}')
                    setTimeout(function(){
                        location.reload()
                    }, 3000)
                }
            },
            error: function(xhr){
                console.log(xhr.responseJSON)
                if (xhr.responseJSON && xhr.responseJSON.msg) {
                    layer.msg(xhr.responseJSON.msg)
                } else {
                    layer.msg('{{ _('未知错误') }}')
                }
            }
        })
        return false;
    })
    {% else %}
    form.on('submit(formSubmit2)', function(obj) {
        layer.msg('数据提交中...',{time:50000})
        if (obj.field.password!=obj.field.repass) {
            layer.msg('{{ _('两次输入的密码不一致，请重新输入') }}')
            $(":input[name='password']").focus()
            return false
        }
        if (rsa_encrypt=='1') {
            // Encrypt with the public key...
            var encrypt = new JSEncrypt()
            encrypt.setPublicKey(public_key)
            var ciphertext = encrypt.encrypt(obj.field.password)
            obj.field.password = ciphertext

            var ciphertext2 = encrypt.encrypt(obj.field.repass)
            obj.field.repass = ciphertext2
        }
        // console.log(obj.field, obj.form.action)
        // return false
        $.ajax({
            type: "POST",
            url: obj.form.action,
            data: obj.field,
            success: function(res) {
                if (res.code==0) {
                    layer.msg('修改成功，请登录', {icon: 1, time: 2000}, function() {
                        location.href = res.next ? res.next : '/passport/login'
                    })
                } else if(res.msg) {
                    layer.msg(res.msg)
                    $(":input[name='username']").focus()
                } else {
                    layer.msg('{{ _('未知错误') }}')
                    setTimeout(function(){
                        location.reload()
                    }, 3000)
                }
            },
            error: function(xhr){
                console.log(xhr.responseJSON)
                if (xhr.responseJSON && xhr.responseJSON.msg) {
                    layer.msg(xhr.responseJSON.msg)
                } else {
                    layer.msg('{{ _('未知错误') }}')
                }
            }
        })
        return false;
    })
    {% end %}

    $("#code").on('click', function () {
        $(this).attr('src', '/passport/captcha.png?t=' + Math.random())
    })
})
</script>
{% end %}
